﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class MaterialCtrl
    {
        MaterialDAO m_MaterialDAO = new MaterialDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "MaterialName";
            cmb.ValueMember = "MaterialId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "MaterialName";
            cmb.ValueMember = "MaterialId";
            cmb.DataPropertyName = "MaterialId";
            cmb.HeaderText = "Nguyên liệu";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "MaterialName";
            cmb.ValueMember = "MaterialId";
            cmb.DataPropertyName = "MaterialId";
            cmb.HeaderText = "Nguyên liệu";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtMaterialId,
                         TextBoxX txtMaterialName,
                         ComboBoxEx cmbCate,
                         ComboBoxEx cmbUnit,
                         TextBoxX txtPrice,
                         CheckBoxX chkState,
                         CheckBoxX chkIsMenu,
                         TextBoxX txtWarningQuantity,
                         TextBoxX txtStockQuantity,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtMaterialId.DataBindings.Clear();
            txtMaterialId.DataBindings.Add("Text", bs, "MaterialId");

            txtMaterialName.DataBindings.Clear();
            txtMaterialName.DataBindings.Add("Text", bs, "MaterialName");

            cmbCate.DataBindings.Clear();
            cmbCate.DataBindings.Add("SelectedValue", bs, "CateId");

            cmbUnit.DataBindings.Clear();
            cmbUnit.DataBindings.Add("SelectedValue", bs, "UnitId");

            txtPrice.DataBindings.Clear();
            txtPrice.DataBindings.Add("Text", bs, "Price");

            chkState.DataBindings.Clear();
            chkState.DataBindings.Add("Checked", bs, "State");

            chkIsMenu.DataBindings.Clear();
            chkIsMenu.DataBindings.Add("Checked", bs, "IsMenu");

            txtWarningQuantity.DataBindings.Clear();
            txtWarningQuantity.DataBindings.Add("Text", bs, "WarningQuantity");

            txtStockQuantity.DataBindings.Clear();
            txtStockQuantity.DataBindings.Add("Text", bs, "StockQuantity");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtMaterialId,
                         TextBoxX txtMaterialName,
                         ComboBoxEx cmbCate,
                         ComboBoxEx cmbUnit,
                         TextBoxX txtPrice,
                         CheckBoxX chkState,
                         CheckBoxX chkIsMenu,
                         TextBoxX txtWarningQuantity,
                         TextBoxX txtStockQuantity,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            txtMaterialId.DataBindings.Clear();
            txtMaterialId.DataBindings.Add("Text", bs, "MaterialId");

            txtMaterialName.DataBindings.Clear();
            txtMaterialName.DataBindings.Add("Text", bs, "MaterialName");

            cmbCate.DataBindings.Clear();
            cmbCate.DataBindings.Add("SelectedValue", bs, "CateId");

            cmbUnit.DataBindings.Clear();
            cmbUnit.DataBindings.Add("SelectedValue", bs, "UnitId");

            txtPrice.DataBindings.Clear();
            txtPrice.DataBindings.Add("Text", bs, "Price");

            chkState.DataBindings.Clear();
            chkState.DataBindings.Add("Checked", bs, "State");

            chkIsMenu.DataBindings.Clear();
            chkIsMenu.DataBindings.Add("Checked", bs, "IsMenu");

            txtWarningQuantity.DataBindings.Clear();
            txtWarningQuantity.DataBindings.Add("Text", bs, "WarningQuantity");

            txtStockQuantity.DataBindings.Clear();
            txtStockQuantity.DataBindings.Add("Text", bs, "StockQuantity");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_MaterialDAO.GetAll();
        }

        public DataTable GetById(String materialId)
        {
            return m_MaterialDAO.GetById(materialId);
        }

        public DataTable GetByLikeId(String materialId)
        {
            return m_MaterialDAO.GetByLikeId(materialId);
        }

        public DataTable GetByName(String materialName)
        {
            return m_MaterialDAO.GetByName(materialName);
        }

        public DataTable GetByOption(String materialId, String materialName, String cateId, String unitId, bool price, double minPrice, double maxPrice)
        {
            return m_MaterialDAO.GetByOption(materialId, materialName, cateId, unitId, price, minPrice, maxPrice);
        }

        public int Add(MaterialInfo info)
        {
            return m_MaterialDAO.Add(info);
        }

        public int Update(MaterialInfo info)
        {
            return m_MaterialDAO.Update(info);
        }

        public int Delete(String materialId)
        {
            return m_MaterialDAO.Delete(materialId);
        }

        public DataRow AddNewRow()
        {
            return m_MaterialDAO.AddNewRow();
        }

        public void AddNewMaterial(DataRow m_Row)
        {
            m_MaterialDAO.AddNewMaterial(m_Row);
        }

        public bool SaveData()
        {
            return m_MaterialDAO.SaveData();
        }
    }
}
